package com.example.javatwo.service.cg.lmx;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.example.javatwo.mapper.cg.djg.PurchaseMapper;
import com.example.javatwo.mapper.cg.lmx.CaiGouRuKuXMapper;
import com.example.javatwo.pojo.cg.djg.Purchase;
import com.example.javatwo.pojo.cg.lmx.CaiGouRuKuX;
import com.example.javatwo.pojo.cg.lmx.CaiGouRuKuZ;

@Service
@Transactional(propagation=Propagation.REQUIRED)
public class CaiGouRuKuXService {

	@Autowired
	private CaiGouRuKuXMapper caiGouRuKuX;
	private PurchaseMapper purchase;
	public String queryMaxId(String date){
		String id=caiGouRuKuX.queryMaxId(date);
		
		if(id!=null){
			id=String.valueOf(Long.parseLong(id+1));
		}else{
			id=date.replace("-","").toString()+"001";
		}
		return id;
	}
	
	public List<CaiGouRuKuX> findAll(){
		return this.caiGouRuKuX.querylist();
	}
	public List<CaiGouRuKuZ>select(){
		return this.caiGouRuKuX.select();
		
	}
	public void insert(CaiGouRuKuZ zhu){
		
		if(caiGouRuKuX.selectCount(zhu.getBillno())!=0){
			caiGouRuKuX.delete(zhu.getBillno());
		}
		caiGouRuKuX.insert(zhu);
		for (CaiGouRuKuX temp : zhu.getZ()) {
			System.out.println("主表的编号："+temp.getBillno()+"要修改的数量"+temp.getSquantity()+"详表的id"+temp.getPromno());
			purchase.udateSum(temp.getSquantity(), temp.getBillno(), temp.getPromno());
		}
		
	}
	public CaiGouRuKuZ page(int i,int j){
		
		return caiGouRuKuX.page((i-1)*j, 1);
	}
	public void del(CaiGouRuKuZ z,String num,String billno,String lineNo){
		caiGouRuKuX.delete(z.getBillno());
		caiGouRuKuX.insert(z);
		// purchase.udateSum(num,billno,lineNo);
	}
	/**
	 * 审核（修改主表的状态）
	 * @throws Exception
	 */
	public void shenhe(Map<String,Object>map)throws Exception{
		//purchase.udateSum(map.get("sum"),map.get("id"), map.get("linno"));
		caiGouRuKuX.shenheupdate(map);
	}
	public String selectId(String id)throws Exception{
		
		
		return caiGouRuKuX.selectId(id);
		
	}
	/**
	 * 转单
	 * @return
	 */
	public List<Purchase> selectZhuanDan(){
		
		return caiGouRuKuX.selectZhuanDan();
	}
	
}
